<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>添加版本</title> {{>/WEB-INF/pages/common/meta_base.part.html}}
{{>/WEB-INF/pages/common/refs_base.part.html}}
{{>/WEB-INF/pages/common/refs_plupload.part.html}}
</head>
<body>
	<!-- <div class="container"> -->
	{{>/WEB-INF/pages/common/layout_header.part.html}}


	<!--Con//Start-->
	<div class="container">
		<ul class="breadcrumb">
			<li><a href="{{ctx}}">首页</a></li>
			<li><a href="{{ctx}}/mam/widget/list-view">模块管理</a></li>
			<li><a href="{{ctx}}/mam/widget/widget-view/{{widgetVO.id}}">{{widgetVO.name}}</a></li>
			<li class="active">添加版本</li>
		</ul>
		<hr class="hr1" />
		<div class="hr-10"></div>
		<div class="container bor-tab bg-st-white">
			<form id="dataForm" name="dataForm" method="post"
				action="{{ctx}}/mam/widgetVersion/add" enctype="multipart/form-data">
				<div>
					<input name="widget_id" class="input-common" type="hidden"
						id="widget_id" value="{{widgetVO.id}}" /> <input name="dependsOn"
						class="input-common" type="hidden" id="dependsOn" value="" /> <input
						name="size" class="input-common" type="hidden" id="size" value="" />
				</div>
				<div class="mar-st-a30">
					<table width="90%" border="0" align="center" cellpadding="0"
						cellspacing="0" class="tab-td-pa">
						<tr>
							<td width="30%" align="right" for="focusedInput">模块名称：</td>
							<td class="bd_sr"><input class="form-control wid50"
								name="name" type="text" value="{{widgetVO.name}}" id="name"
								readonly="readonly" required /></td>
						</tr>
						<tr>
							<td width="30%" align="right" for="focusedInput">模块标识：</td>
							<td class="bd_sr"><input class="form-control wid50"
								type="text" name="identify" value="{{widgetVO.identify}}"
								id="identifier" readonly="readonly" /></td>
						</tr>
						<tr>
							<td width="30%" align="right" for="focusedInput">模块包：</td>
							<td class="bd_sr">
								<div id="packUploadContainer"></div>
							</td>
						</tr>
						<tbody id="toboyId" style="display: none">
							<tr>
								<td width="30%" align="right" for="focusedInput">构建号：</td>
								<td class="bd_sr"><input class="form-control wid50"
									name="build" value="" readonly="readonly" id="build" required /></td>
							</tr>
							<tr>
								<td width="30%" align="right" for="focusedInput">模块版本：</td>
								<td class="bd_sr"><input class="form-control wid50"
									readonly="readonly" name="version" value="" id="version"
									required /></td>
							</tr>
							<tr>
								<td align="right" class="control-group">版本简介：</td>
								<td colspan="3" class="bd_sr"><textarea
										class="form-control wid50" id="release_not" name="release_not"
										cols="" rows=""></textarea></td>
							</tr>
						</tbody>
					</table>
				</div>

				<div class="hr-10"></div>
				<div class="text-center">
					<button type="button" class="btn btn-primary btn-small tls"
						id="submitBut">保存</button>
					<button type="button" class="btn btn-common btn-small tls"
						onclick="history.back();">返回</button>
				</div>
			</form>
			<div class="hr-20"></div>
		</div>
	</div>
	{{>/WEB-INF/pages/common/layout_footer.part.html}}
</body>
</html>
<script type="text/javascript">
	var popover = function(inputId, content) {
		var options = {
			placement : "bottom",
			content : content
		};
		$('#' + inputId).attr("data-toggle", "popover");
		$('#' + inputId).popover(options);
		$('#' + inputId).popover('show');
	}
	$(function() {
		var uploader = buildPlupload({
			id : "packUploadContainer",
			elmName : "boundle",
			filters : [ {
				title : "模块包",
				extensions : "zip"
			} ],
			targetUrl : "{{ctx}}/mam/attachment/upload",
			defaultVal : "",
			btnName : "上传模块包"
		});

		uploader.bind('FileUploaded', function(up, files, info) {
			var obj = eval("(" + info.response + ")");
			var id = obj.id;
			$('#packUploadContainerInput').val(id);
			var size = $('#packUploadContainerSize').val();
			$('#size').val(size);

			var tel = /.zip$/;
			var value = jQuery("#packUploadContainerName").val();
			if (value != null && value != "") {
				if (!tel.test(value)) {
					popover("packUploadContainerBtn", "请上传zip包！");
					$('#packUploadContainerInput').val("");
					return false;
				}
			}
			readFileAction(id);

		});

		$("#submitBut").click(function() {
			if (jQuery("#packUploadContainerInput").val() == "") {
				popover("packUploadContainerBtn", "请上传正确的模块包");
				return false;
			}
			var oldVersion = "{{versionVO.version}}";
			var version = jQuery("#version").val();
			var oldReplace = oldVersion.replace(/\./g, "");
			var newReplace = version.replace(/\./g, "");
			if (parseInt(oldReplace) >= parseInt(newReplace)) {
				popover("version", "版本号必须大于最新版本！请修改");
				return false;
			}
			dataForm();
		});
	});

	function dataForm() {
		var data = $('#dataForm').serialize();
		$
				.ajax({
					async : true,
					type : "POST",
					url : "{{ctx}}/mam/widgetVersion/add",
					data : data,
					dataType : 'json',
					success : function(result, textStatus) {
						if (result.result == "success") {
							var url="{{ctx}}/mam/widget/widget-view/"
								+ result.widgetid;
							bootbox
									.alert(
											"保存成功！",
											function() {
												window.location.href = url;
											});
						} else {
							bootbox.alert("添加失败，请稍后再试！");
						}
					},
					error : function(data, status, e) {
						bootbox.alert("系统繁忙，请稍后再试！");
					}
				});

	}

	function readFileAction(id) {
		$.ajax({
			async : true,
			type : "GET",
			url : "{{ctx}}/mam/attachment/readfile/" + id,
			dataType : 'json',
			success : function(data, textStatus) {
				var cloneidentifier = $("#identifier").val();
				var identifier = data.identifier;
				if (cloneidentifier != identifier) {
					$('#packUploadContainerInput').val("");
					popover("packUploadContainerBtn", "模块标识不正确，请重新上传模块包！");
				} else {
					identifierVersion(data);
				}
			}
		});
	}

	//检查版本号是否存在
	function identifierVersion(options) {
		$.ajax({
			async : false,
			type : "GET",
			url : "{{ctx}}/mam/widgetVersion/" + options.identifier + "/"
					+ options.version + "/",
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "true") {
					$('#packUploadContainerInput').val("");
					popover("packUploadContainerBtn", "模块版本已经存在，请重新上传模块包！");
				} else {
					jQuery("#version").val(options.version);
					jQuery("#dependsOn").val(options.dependsOn);
					if (options.build != null && options.build != "") {
						jQuery("#build").val(options.build);
					}
					jQuery("#release_not").val(options.releaseNote);
					if (data.hidden == "true") {
						jQuery("#hiddenYES").attr("checked", "true");
					} else {
						jQuery("#hiddenNO").attr("checked", "true");
					}
					$('#toboyId').show();
				}
			},
			error : function(data, status, e) {
				bootbox.alert("检查版本号失败，请稍后再试！");
			}
		});
	}
</script>
